package com.cskaoyan.service;

import com.cskaoyan.bean.BO.WxCommentCountBo;
import com.cskaoyan.bean.BO.WxCommentListBO;
import com.cskaoyan.bean.PO.MarketComment;
import com.cskaoyan.bean.PO.MarketCommentExample;
import com.cskaoyan.bean.VO.WxCommentCountVo;
import com.cskaoyan.bean.VO.WxCommentVO;
import com.cskaoyan.bean.common.CommonData;
import com.cskaoyan.mapper.MarketCommentMapper;
import com.cskaoyan.mapper.WxCommentMapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

/**
 * @author linhanxin
 * @date 2022/8/17.
 */
@Service
public class WxCommentServiceImpl implements WxCommentService {
    @Autowired
    MarketCommentMapper marketCommentMapper;
    @Autowired
    WxCommentMapper wxCommentMapper;
    @Override
    public CommonData<WxCommentVO> list(WxCommentListBO wxCommentListBO) {
        PageHelper.startPage(wxCommentListBO.getPage(), wxCommentListBO.getLimit());
        List<WxCommentVO> list = wxCommentMapper.select(wxCommentListBO);
        PageInfo <WxCommentVO> pageInfo = new PageInfo<>(list);
        return CommonData.data(pageInfo);
    }

    @Override
    public WxCommentCountVo count(WxCommentCountBo commentCountBo) {
        MarketCommentExample example1 = new MarketCommentExample();
        MarketCommentExample.Criteria criteria1 = example1.createCriteria();
        MarketCommentExample example2 = new MarketCommentExample();
        MarketCommentExample.Criteria criteria2 = example2.createCriteria();
        //获取请求中携带的type和valueId参数
        byte type = (byte) commentCountBo.getType();
        Integer valueId = commentCountBo.getValueId();
        criteria1.andTypeEqualTo(type).andValueIdEqualTo(valueId).andHasPictureEqualTo(true);
        Integer hasPicCount = wxCommentMapper.countByExample(example1);
        //这是查询有内容的评价的
        criteria2.andTypeEqualTo(type).andValueIdEqualTo(valueId).andContentIsNotNull();
        Integer allCount=wxCommentMapper.countByExample(example2);
        WxCommentCountVo wxCommentCountVo = new WxCommentCountVo();
        wxCommentCountVo.setAllCount(allCount);
        wxCommentCountVo.setHasPicCount(hasPicCount);
        return wxCommentCountVo;
    }

    @Transactional
    @Override
    public MarketComment post(MarketComment marketComment,Integer userId) {
        marketComment.setUserId(userId);
        marketComment.setAddTime(new Date());
        marketComment.setUpdateTime(new Date());
        if (marketComment.getContent() == null) {
            return null;
        }
        int i = wxCommentMapper.insertWithoutId(marketComment);
        if (i != 1) {
            try {
                int j = 1 / 0;
            } catch (Exception e) {
                return null;
            }
        }
        Integer id = marketComment.getId();
        MarketComment commentVo = wxCommentMapper.selectById(id);
        return commentVo;
    }
}
